package com.jcl.recruit.email.logic;

import com.jcl.recruit.core.config.SystemConfig;
import com.jcl.recruit.core.config.TemplateConfig;
import com.jcl.recruit.server.emessage.MailMessageVo;
import com.jcl.recruit.server.emessage.MessageServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName SendEmailHandler
 * @Description TODO
 * @Author yyliu
 * @Date 2018/9/18 10:01
 * @Version 1.0
 **/
@Component
public class SendEmailHandler {

    @Autowired
    private TemplateConfig templateConfig;

    @Autowired
    private MessageServer messageServer;

    public int sendEmailRegisterCode(String receiver, String code) {

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("logo", SystemConfig.getSystemLogo());
        params.put("code", code);

        return sendEmail(params,
                TemplateInfo.TEMPLATE_CERTIFY_CODE,
                TemplateInfo.TEMPLATE_CERTIFY_SUBJECT,
                receiver);
    }

    public int sendEmailRegisterCorp(String receiver,
                                     String corpName,
                                     String account,
                                     String password) {

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("logo", SystemConfig.getSystemLogo());
        params.put("corpName", corpName);
        params.put("account", account);
        params.put("password", password);

        return sendEmail(params,
                TemplateInfo.TEMPLATE_REGISTER_CORP,
                TemplateInfo.TEMPLATE_REGISTER_CORP_SUBJECT,
                receiver);
    }


    /* *
     * @Author yyliu
     * @Description //TODO
     * @Date 12:32 2018/9/19
     * @Param [params, templateId, subject, receiver]
     * @return int
     **/
    private int sendEmail(Map<String, Object> params,
                          String templateId,
                          String subject,
                          String receiver) {

        String content = templateConfig.templateContent(templateId, params);
        MailMessageVo info = new MailMessageVo();
        info.setSubject(subject);
        info.setTo(receiver);
        info.setContent(content);
        messageServer.send(info);
        //return emailConfig.sendHtmlMail(info);
        return 1;
    }


    public static class TemplateInfo {

        public static final String TEMPLATE_CERTIFY_SUBJECT = "聚才林注册验证码";
        public static final String TEMPLATE_REGISTER_CORP_SUBJECT = "聚才林企业注册通知";
        public static final String TEMPLATE_CERTIFY_CODE = "email-invite-code-template";
        public static final String TEMPLATE_REGISTER_CORP = "email-register-corp-template";

    }
}